class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res = {-1,-1};
        int left = 0;
        int right = nums.size()-1;
        while(left < right)
        {
            int mid = (left+right) / 2;
            if(nums[mid] >= target)
            {
                right = mid;
            }
            else
            {
                left = mid + 1;
            }
        }
        if(nums.size()==0 || nums[left] != target) return res;
        res[0] = left;
        right = nums.size();
        while(left < right)
        {
            int mid = (left+right) / 2;
            if(nums[mid] <= target)
            {
                left = mid + 1;
            }
            else
            {
                right = mid;
            }
        }
        res[1] = right-1;
        return res;
    }
};

// O(n)
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        vector<int> res = {-1,-1};
        int left = 0;
        int right = nums.size()-1;
        while(left <= right)
        {
            int mid = (left+right) / 2;
            if(target == nums[mid])
            {
                int lidx = mid;
                int ridx = mid;
                while((lidx>=0 && nums[lidx]==target))
                {
                    res[0] = lidx;
                    lidx--;
                }
                while(ridx<nums.size() && nums[ridx]==target)
                {
                    res[1] = ridx;
                    ridx++;
                }
                return res;
            }
            else if(target > nums[mid])
            {
                left = mid + 1;
            }
            else
                right = mid - 1;
        }

        return res;
    }
};
// O(nlogn)